package com.tsingtree.modular.controller.view;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.model.params.NanPopupAdvertItemParam;
import com.tsingtree.modular.model.params.NanPopupAdvertParam;
import com.tsingtree.modular.model.result.NanPopupAdvertResult;
import com.tsingtree.modular.service.NanPopupAdvertService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 弹窗广告控制器
 *
 * @author jagger
 * @since 2025-10-22
 */
@Controller
@RequestMapping("/nanPopupAdvert")
public class NanPopupAdvertController extends BaseController {

    private String PREFIX = "/nanPopupAdvert";

    @Autowired
    private NanPopupAdvertService nanPopupAdvertService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanPopupAdvert.html";
    }

    /**
     * 跳转到新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanPopupAdvert_add.html";
    }

    /**
     * 跳转到编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanPopupAdvert_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanPopupAdvertParam nanPopupAdvertParam) {
        // 处理 JSON 格式的 items 参数
        if (StringUtils.hasText(nanPopupAdvertParam.getItemsJson())) {
            List<NanPopupAdvertItemParam> items = JSON.parseObject(
                nanPopupAdvertParam.getItemsJson(),
                new TypeReference<List<NanPopupAdvertItemParam>>() {}
            );
            nanPopupAdvertParam.setItems(items);
        }

        this.nanPopupAdvertService.add(nanPopupAdvertParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanPopupAdvertParam nanPopupAdvertParam) {
        // 处理 JSON 格式的 items 参数
        if (StringUtils.hasText(nanPopupAdvertParam.getItemsJson())) {
            List<NanPopupAdvertItemParam> items = JSON.parseObject(
                nanPopupAdvertParam.getItemsJson(),
                new TypeReference<List<NanPopupAdvertItemParam>>() {}
            );
            nanPopupAdvertParam.setItems(items);
        }

        this.nanPopupAdvertService.update(nanPopupAdvertParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    @RequestMapping("/delete")
    @Permission
    @ResponseBody
    public ResponseData delete(NanPopupAdvertParam nanPopupAdvertParam) {
        this.nanPopupAdvertService.delete(nanPopupAdvertParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanPopupAdvertParam nanPopupAdvertParam) {
        NanPopupAdvertResult detail = this.nanPopupAdvertService.findBySpec(nanPopupAdvertParam);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanPopupAdvertParam nanPopupAdvertParam) {
        return this.nanPopupAdvertService.findPageBySpec(nanPopupAdvertParam);
    }

    /**
     * 立即发送广告
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/sendNow")
    public ResponseData sendNow(NanPopupAdvertParam nanPopupAdvertParam) {
        // 查询广告详情
        NanPopupAdvertResult advert = this.nanPopupAdvertService.findBySpec(nanPopupAdvertParam);

        if (advert == null) {
            return ResponseData.error("广告不存在");
        }

        if (advert.getType() != 2) {
            return ResponseData.error("只有即时弹窗类型的广告才能立即发送");
        }

        // 发送消息到 RabbitMQ 队列
        try {
            amqpTemplate.convertAndSend("popup_advert_queue", advert.getId());
            return ResponseData.success("推送任务已发送，系统将向所有使用该设备的在线用户推送弹窗广告");
        } catch (Exception e) {
            return ResponseData.error("推送失败: " + e.getMessage());
        }
    }

}

